package com.crm.providerdept8001hystrix.controller;

import com.crm.api.entity.Dept;
import com.crm.providerdept8001hystrix.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by yangShen
 * 2019/8/16 13:00
 *
 * 服务熔断举例：方法级别
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

//    @RequestMapping(value = "/dept/add",method = RequestMethod.POST)
//    public boolean addDept(@RequestBody Dept dept){
//        return deptService.addDept(dept);
//    }


    //一旦调用服务方法失败 并抛出了错误信息后，会自动调用@HystrixCommand标记好的fallbackMethod调用类中的指定方法
    //测试方式：直接调用该路径
    //这里打断点，逗留时间久了就会抛异常，除非快速跳过
    @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "processHystrix_Get")
    public Dept get(@PathVariable("id") Long id){
        Dept dept = deptService.get(id);
        if (null == dept){
            throw new RuntimeException("该ID"+ id +"没有对应的信息");
        }
        return dept;
    }


//    @RequestMapping(value = "/dept/list",method = RequestMethod.GET)
//    public List<Dept> list(){
//        return deptService.list();
//    }

    public Dept processHystrix_Get(@PathVariable("id") Long id){
        return new Dept().setDeptNo(id).setDeptName("该ID:"+id+"没有对应的信息，null，--@HystrixCommand")
                .setDbSource("no this database in mysql");
    }

}
